package com.gimi.cloud.bbp.manage.consumer.service.task.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.log.LogScheduleJobDTO;
import com.gimi.cloud.bbp.dto.task.ScheduleJobDTO;
import com.gimi.cloud.bbp.manage.consumer.feign.ScheduleJobFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.task.ScheduleJobService;

/**
 * 定时任务相关service实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-02-06
 **/
@Service
public class ScheduleJobServiceImpl implements ScheduleJobService {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobServiceImpl.class);

    private ScheduleJobFeignService scheduleJobFeignService;

    @Autowired
    public ScheduleJobServiceImpl(ScheduleJobFeignService scheduleJobFeignService) {
        this.scheduleJobFeignService = scheduleJobFeignService;
    }

    @Override
    public Response getTaskList(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
    	 return scheduleJobFeignService.getTaskList(scheduleJobDTO);
    }

    @Override
    public Response getTaskInfo(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
    	 return scheduleJobFeignService.getTaskInfo(scheduleJobDTO);
    }

    @Override
    public Response addTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null != scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId不为空，添加定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.addTask(scheduleJobDTO);
    }

    @Override
    public Response updateTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null == scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId为空，更新定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.updateTask(scheduleJobDTO);
    }

    @Override
    public Response deleteTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null == scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId为空，删除定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.deleteTask(scheduleJobDTO);
    }

    @Override
    public Response runTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null == scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId为空，执行定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.runTask(scheduleJobDTO);
    }

    @Override
    public Response pauseTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null == scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId为空，暂停定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.pauseTask(scheduleJobDTO);
    }

    @Override
    public Response resumeTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null == scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId为空，恢复定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.resumeTask(scheduleJobDTO);
    }

    @Override
    public Response recreateTask(HttpServletRequest request, HttpServletResponse response, ScheduleJobDTO scheduleJobDTO) {
        if(null == scheduleJobDTO.getJobId()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"JobId为空，重持久化定时任务失败");
            return resp;
        }
        return scheduleJobFeignService.recreateJob(scheduleJobDTO);
    }

    @Override
    public Response getTaskLogList(HttpServletRequest request, HttpServletResponse response, LogScheduleJobDTO dto) {
        return scheduleJobFeignService.getTaskLogList(dto);
    }
}
